#-*-coding:utf-8-*-
__author__ = 'toriycn'

from rediscluster import StrictRedisCluster
import redis
class RedisHelper:

    def __init__(self,host,port,chan_sub,chan_pub):
        #startup_nodes = [{"host": host, "port": port}]
        #self.rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
        self.rc = redis.Redis(host= host,port = port)
        self.chan_sub = chan_sub
        self.chan_pub = chan_pub

    def get(self,key):
        return self.rc.get(key)

    def set(self,key,value):
        self.rc.set(key,value)

    def keys(self,pattern="*"):
        return self.rc.keys(pattern)

    def delete(self,key):
        return self.rc.delete(key)


    def public(self,msg):
        self.rc.publish(self.chan_pub,msg)
        return True

    def subscribe(self):
        pub = self.rc.pubsub()
        pub.subscribe(self.chan_sub)
        pub.parse_response()
        return pub


if __name__ == '__main__':
    v={
        "ip":"127.0.0.1",
        "user":"dl"
    }
    r = RedisHelper("127.0.0.1",6379,"dl","dl")
    # r.set("HOSTINFO::127.0.0.1::",v)
    # r.set("HOSTINFO::127.0.0.2::",v)
    # r.set("HOSTINFO::127.0.0.3::",v)
    print(r.keys("HOSTINFO::"))
    hosts = []
    #从redis获取所有的主机列表,目前不考虑分页
    serverInfo = r.keys("HOSTINFO::*")
    for v in serverInfo:
        print(v)
        hosts.append(eval(r.get(v)))

    print(hosts)
